import { Injectable } from '@angular/core';
import TileWMS from 'ol/source/TileWMS.js';
import { Tile as TileLayer, Vector as VectorLayer } from 'ol/layer.js';
import Projection from 'ol/proj/Projection';
import WTMSTilegrid from 'ol/tilegrid/WMTS';
import WMTS from 'ol/source/WMTS.js';
import OSM from 'ol/source/OSM.js';
import Image from 'ol/source/Image';
import ImageWMS from 'ol/source/ImageWMS';
import { Observable, Subject } from 'rxjs';
import { ConfigService } from 'app/config/config.service';

// const GEOSERVICE = 'http://192.168.2.26:8080';
const GEOSERVICE = 'http://192.168.0.59:8080';
// const GEOSERVICE = 'http://192.168.0.57:8080';
// /geoserver/gwc/demo/test:testone?gridSet=EPSG:4326&format=image/png

@Injectable()
export class MapViewService {
    geoServiceUrl = this.config.apiUrl.gis;
    centerCoords = [107.085144, 20.951674];

    constructor(private config: ConfigService) {}
    getZoneLayer(visible) {
        const url = `${this.geoServiceUrl}/geoserver/HaLong/wms`;
        // tslint:disable-next-line:max-line-length
        // `${this.geoServiceUrl}/geoserver/HaLong/wms?service=WMS&version=1.1.0&request=GetMap&layers=HaLong%3Azone&bbox=106.439320466014%2C20.4261394754895%2C108.222113597408%2C21.6631621669&width=768&height=532&srs=EPSG%3A4326&format=application/openlayers`;

        const tile = new TileWMS({
            url,
            crossOrigin: 'Anonymous',
            params: {
                FORMAT: 'image/png',
                VERSION: '1.1.1',
                tiled: true,
                LAYERS: 'HaLong:zone',
                exceptions: 'application/vnd.ogc.se_inimage',
                tilesOrigin: 106.439320466014 + ',' + 20.4261394754895
            }
        });
        return new TileLayer({
            visible,
            source: tile,
            zIndex: 15
        });
    }
    getTrafficDataLayer(visible) {
        // tslint:disable-next-line:max-line-length
        const url = `${this.geoServiceUrl}/geoserver/HaLong/wms`;
        // const url = `${
        //     this.geoServiceUrl
        //     // tslint:disable-next-line: max-line-length
        // tslint:disable-next-line:max-line-length
        //     }/geoserver/HaLong/wms?service=WMS&version=1.1.0&request=GetMap&layers=HaLong%3AVMS_export_view&bbox=107.035943952751%2C20.9500827003564%2C107.086116699826%2C20.9764437999415&width=768&height=403&srs=EPSG%3A4326&format=application/openlayers`;
        const tile = new TileWMS({
            url,
            /* params: {
                service: 'WMS',
                version: '1.1.0',
                request: 'GetMap',
                layers: 'VietnamTest:VietnamTest',
                bbox: 101.888824462891 + ',' + 8.33772850036621 + ',' + 109.493980407715 + ',' + 23.5692386627197,
                width: 383,
                height: 768,
                srs: 'EPSG:4326',
                format: 'application/openlayers'
            }, */
            crossOrigin: 'Anonymous',
            params: {
                FORMAT: 'image/png',
                VERSION: '1.1.1',
                tiled: true,
                LAYERS: 'HaLong:VMS_export_view',
                exceptions: 'application/vnd.ogc.se_inimage',
                tilesOrigin: 107.035943952751 + ',' + 20.9500827003564
            }
        });
        return new TileLayer({
            visible: true,
            source: tile,
            zIndex: 1
        });
    }

    getBasicMapLayerGeo() {
        return new TileLayer({
            visible: true,
            source: new TileWMS({
                url: `${this.geoServiceUrl}/geoserver/HaLong/wms`,
                params: {
                    FORMAT: 'image/png',
                    VERSION: '1.1.1',
                    tiled: true,
                    LAYERS: 'HaLong:viet',
                    exceptions: 'application/vnd.ogc.se_inimage',
                    tilesOrigin: -0.0000089831528412 + ',' + -0.000008983152841
                }
            })
        });
    }
    /**
     * http://192.168.0.57:8080/geoserver/VietnamTest/wms?
     * service=WMS&version=1.1.0&request=GetMap&
     * layers=VietnamTest:VietnamTest&
     * bbox=101.888824462891,8.33772850036621,109.493980407715,23.5692386627197&width=383&height=768&srs=EPSG:4326&format=application/openlayers#toggle
     */
    getBasicMapLayer() {
        return new TileLayer({
            title: 'basicMap',
            /* source: new WMTS({
                url: `/geoserver/VietnamTest/wms`,
                layer: 'VietnamTest:VietnamTest',
                matrixSet: 'EPSG:4326',
                format: 'application/openlayers',
                projection: new Projection({
                    code: 'EPSG:4326',
                    units: 'degrees',
                    axisOrientation: 'neu'
                }),
                tileGrid: new WTMSTilegrid({
                    tileSize: [256, 256],
                    extent: [51.2828982, 22.6318115, 56.4119588, 26.3112911],
                    origin: [-180.0, 90],
                    resolutions: [
                        0.703125,
                        0.3515625,
                        0.17578125,
                        0.087890625,
                        0.0439453125,
                        0.02197265625,
                        0.010986328125,
                        0.0054931640625,
                        0.00274658203125,
                        0.001373291015625,
                        6.866455078125e-4,
                        3.4332275390625e-4,
                        1.71661376953125e-4,
                        8.58306884765625e-5,
                        4.291534423828125e-5,
                        2.1457672119140625e-5,
                        1.0728836059570312e-5,
                        5.364418029785156e-6,
                        2.682209014892578e-6,
                        1.341104507446289e-6,
                        6.705522537231445e-7,
                        3.3527612686157227e-7
                    ],
                    matrixIds: [
                        'EPSG:4326:0',
                        'EPSG:4326:1',
                        'EPSG:4326:2',
                        'EPSG:4326:3',
                        'EPSG:4326:4',
                        'EPSG:4326:5',
                        'EPSG:4326:6',
                        'EPSG:4326:7',
                        'EPSG:4326:8',
                        'EPSG:4326:9',
                        'EPSG:4326:10',
                        'EPSG:4326:11',
                        'EPSG:4326:12',
                        'EPSG:4326:13',
                        'EPSG:4326:14',
                        'EPSG:4326:15',
                        'EPSG:4326:16',
                        'EPSG:4326:17',
                        'EPSG:4326:18',
                        'EPSG:4326:19',
                        'EPSG:4326:20',
                        'EPSG:4326:21'
                    ]
                }),
                wrapX: true
            }) */
            source: new OSM({
                wrapX: true
            })
        });
    }
    // getZoneMapLayer() {
    //     const url = null;
    //     const tile = new TileWMS({
    //         url,
    //         crossOrigin: 'Anonymous',
    //         serviceType:
    //     });
    //     return new TileLayer({
    //         title: 'basicMap',
    //         source: new OSM({
    //             wrapX: true
    //         })
    //     });
    // }

    /**
     * gis 定位功能
     */
    createLocationObservable(): Observable<any> {
        return Observable.create(observer => {
            // const { next, error } = observer;
            const geoOptions = {
                enableHighAccuracy: true,
                maximumAge: 30000,
                timeout: 5000
            };

            if ('geolocation' in navigator) {
                navigator.geolocation.getCurrentPosition(
                    position => observer.next(position),
                    error => observer.error('locate faild'),
                    geoOptions
                );
            } else {
                observer.error('locate faild');
            }
        });
    }
}
